package com.ilarele.shopping.client.provider;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;

import com.ilarele.shopping.client.util.ImageUtilities;

public class ShoppingManager {
	static final int BOOK_COVER_WIDTH = 100;
	static final int BOOK_COVER_HEIGHT = 120;

	private static String sItemIdSelection;
	private static String sItemSelection;

	private static String[] sArguments1 = new String[1];
	private static String[] sArguments3 = new String[3];

	private static final String[] PROJECTION_ID_IID = new String[] {
			ShoppingGenericItemDB._ID, ShoppingGenericItemDB.INTERNAL_ID };
	private static final String[] PROJECTION_ID = new String[] { ShoppingGenericItemDB._ID };

	static {
		StringBuilder selection = new StringBuilder();
		selection.append(ShoppingGenericItemDB.INTERNAL_ID);
		selection.append("=?");
		sItemIdSelection = selection.toString();

		selection = new StringBuilder();
		selection.append(sItemIdSelection);

		// TODO: barcode
		// selection.append(" OR ");
		// selection.append(ShoppingDBItem.INTERNAL_ID);
		// selection.append("=?");
		sItemSelection = selection.toString();
	}

	public static String findBookId(ContentResolver contentResolver, String id) {
		String internalId = null;
		Cursor c = null;

		try {
			final String[] arguments3 = sArguments3;
			arguments3[0] = arguments3[1] = arguments3[2] = id;
			c = contentResolver.query(ShoppingGenericItemDB.CONTENT_URI,
					PROJECTION_ID_IID, sItemSelection, arguments3, null);
			if (c.getCount() > 0) {
				if (c.moveToFirst()) {
					internalId = c
							.getString(c
									.getColumnIndexOrThrow(ShoppingGenericItemDB.INTERNAL_ID));
				}
			}
		} finally {
			if (c != null)
				c.close();
		}

		return internalId;
	}

	public static boolean bookExists(ContentResolver contentResolver, String id) {
		boolean exists;
		Cursor c = null;

		try {
			final String[] arguments3 = sArguments3;
			arguments3[0] = arguments3[1] = arguments3[2] = id;
			c = contentResolver.query(ShoppingGenericItemDB.CONTENT_URI,
					PROJECTION_ID, sItemSelection, arguments3, null);
			exists = c.getCount() > 0;
		} finally {
			if (c != null)
				c.close();
		}

		return exists;
	}

	public static boolean deleteBook(ContentResolver contentResolver,
			String itemId) {
		final String[] arguments1 = sArguments1;
		arguments1[0] = itemId;
		int count = contentResolver.delete(ShoppingGenericItemDB.CONTENT_URI,
				sItemIdSelection, arguments1);
		ImageUtilities.deleteCachedCover(itemId);
		return count > 0;
	}

	public static ShoppingGenericItemDB findItem(
			ContentResolver contentResolver, String id) {
		ShoppingGenericItemDB item = null;
		Cursor c = null;

		try {
			sArguments1[0] = id;
			c = contentResolver.query(ShoppingGenericItemDB.CONTENT_URI, null,
					sItemIdSelection, sArguments1, null);
			if (c.getCount() > 0) {
				if (c.moveToFirst()) {
					item = ShoppingGenericItemDB.fromCursor(c);
				}
			}
		} finally {
			if (c != null)
				c.close();
		}

		return item;
	}

	public static ShoppingGenericItemDB findBook(
			ContentResolver contentResolver, Uri data) {
		ShoppingGenericItemDB item = null;
		Cursor c = null;

		try {
			c = contentResolver.query(data, null, null, null, null);
			if (c.getCount() > 0) {
				if (c.moveToFirst()) {
					item = ShoppingGenericItemDB.fromCursor(c);
				}
			}
		} finally {
			if (c != null)
				c.close();
		}

		return item;
	}
}
